package com.ayoka.ui.assignment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.ayoka.R;
import com.ayoka.base.BaseFragment;
import com.ayoka.base.BasePresenter;
import com.ayoka.bean.CourseAssignmentBean;
import com.ayoka.bean.CourseAssignmentBean.QuestionBean;
import com.ayoka.bean.CourseAssignmentBean.RequestAdmixtureAssignmentBean;
import com.ayoka.databinding.FragmentCourseAssignmentAdmixtureBinding;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class CourseAssignmentAdmixtureFragment extends BaseFragment<FragmentCourseAssignmentAdmixtureBinding, BasePresenter> {

    public static CourseAssignmentAdmixtureFragment newInstance(CourseAssignmentBean courseAssignmentBean) {
        CourseAssignmentAdmixtureFragment assignmentFragment = new CourseAssignmentAdmixtureFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("CourseAssignmentBean", courseAssignmentBean);
        assignmentFragment.setArguments(bundle);
        return assignmentFragment;
    }

    protected CourseAssignmentBean courseAssignmentBean;

    private List<QuestionBean> Questions;
    private int currentIndex;
    private Fragment currentFragment;

    @Override
    protected void configBundleArguments() {
        courseAssignmentBean = (CourseAssignmentBean) getArguments().getSerializable("CourseAssignmentBean");
        if (courseAssignmentBean != null) {
            Questions = courseAssignmentBean.Questions;
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_course_assignment_admixture;
    }

    @Override
    protected void initViews(View view) {

    }

    @Override
    public void initData() {
        configFragment(currentIndex);
    }

    private void configFragment(int currentIndex) {
        QuestionBean questionBean = Questions.get(currentIndex);
        RequestAdmixtureAssignmentBean answerBean = new RequestAdmixtureAssignmentBean();
        answerBean.QuestionID = questionBean.ID;
        questionBean.AdmixtureAnswer = answerBean;
        int assignmentType = questionBean.QuestionType;
        String indexString = (currentIndex + 1) + "/" + Questions.size();
        if (assignmentType == CourseAssignmentBean.SINGLE_CHOICE) {
            answerBean.OptionAnswer = new ArrayList<>();
            currentFragment = CourseAssignmentChoiceFragment.newInstance(questionBean, indexString, CourseAssignmentBean.SINGLE_CHOICE);
        } else if (assignmentType == CourseAssignmentBean.MULTI_CHOICE) {
            answerBean.OptionAnswer = new ArrayList<>();
            currentFragment = CourseAssignmentChoiceFragment.newInstance(questionBean, indexString, CourseAssignmentBean.MULTI_CHOICE);
        } else if (assignmentType == CourseAssignmentBean.GRADE) {
            answerBean.ScoreItemAnswer = new ArrayList<>();
            currentFragment = CourseAssignmentGradeFragment.newInstance(questionBean, indexString);
        } else if (assignmentType == CourseAssignmentBean.FILL) {
            currentFragment = CourseAssignmentInputFragment.newInstance(questionBean, indexString);
        } else if (assignmentType == CourseAssignmentBean.NO_OPERATION) {
            currentFragment = CourseAssignmentNoOperationFragment.newInstance(questionBean, indexString);
        }
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frameLayout, currentFragment).commitAllowingStateLoss();
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }


    public void showNextQuestion() {
        if (getActivity() == null || getActivity().isFinishing()) return;
        if (currentIndex == Questions.size() - 1) {
            List<CourseAssignmentBean.RequestAdmixtureAssignmentBean> requestResultData = new ArrayList<>();
            for (CourseAssignmentBean.QuestionBean question : courseAssignmentBean.Questions) {
                requestResultData.add(question.AdmixtureAnswer);
            }
            Gson gson = new Gson();
            ((CourseAssignmentDetailActivity) getActivity()).submitCourseAssignment(null);
        } else {
            configFragment(++currentIndex);
        }
    }
}
